
* This file assembles the pooled cross-sections of the Väst-SOM survey
* and estimates the gap in nascent ambition, the left-hand side of 
* Figure 7, as well as the gaps in interest and knowing a politcian in 
* Appendix Figure 11.

* The Väst-SOM data was ordered from snd.se, which is a depository for 
* Swedish research data accessible subject to a brief verification of the
* purpose of use by the responsible researchers for each dataset.

* Put the relevant datasets on the hard drive, open each dataset and
* rename the relevant variables in each year, then append.

cd "C:\Users\Johanna\Documents\Väst som analysis workers data\"

use "1996 vastsom.DTA", clear 

* Compare nascent ambition between self-identified workers and non-workers
* use age and student status to set the sample to the paid labor force
* also measure party and whether people as aquainted with a local politician.


ren V5 sex
ren V491 muni
ren V6 birth_yr
ren V123 party

tab V116
count if V116==1
gen interest = 1 if V116==1 | V116==2
replace interest = 0 if V116==3 | V116==4

tab V244
gen ambkom = 1 if V244==1
replace ambkom = 0 if V244!=. & ambkom!=1
* bara kommunpol

tab V224
gen bekantpol = 1 if V224==1
replace bekantpol = 0 if V224!=. & bekantpol!=1
* bara kommunpol

ren V544 class
tab class
gen workerselfid = 1 if class==4 | class==5
replace workerselfid = 0 if workerselfid!=1 & class!=.

tab V540
gen stud = 1 if V540==8
replace stud = 0 if stud!=1 & V540!=.

gen workerfam = 1 if V554==1
replace workerfam = 0 if workerfam!=1 & V554!=.

gen year = 1996

keep birth_yr ambkom bekantpol class workerselfid workerfam stud party year sex muni interest

save "C:\Users\Johanna\Dropbox\Worker rep\kolfu som analysis\ambition vast som.dta", replace

** 1997 **

use "1997 vastsom.DTA", clear 

ren V5 sex
ren V378 muni
ren V6 birth_yr
ren V126 party

tab V118
count if V118==1
gen interest = 1 if V118==1 | V118==2
replace interest = 0 if V118==3 | V118==4


gen ambkom = 1 if V238==1 | V238==2
replace ambkom = 0 if V238!=. & ambkom!=1
* bara kommunpol

tab V162
gen bekantpol = 1 if V162==1
replace bekantpol = 0 if V162!=. & bekantpol!=1
* bara kommunpol

ren V412 class
tab class
gen workerselfid = 1 if class==4 | class==5
replace workerselfid = 0 if workerselfid!=1 & class!=.

tab V408
gen stud = 1 if V408==8
replace stud = 0 if stud!=1 & V408!=.

gen workerfam = 1 if V417==1
replace workerfam = 0 if workerfam!=1 & V417!=.

gen year = 1997

keep birth_yr ambkom bekantpol class workerselfid workerfam stud party year sex muni interest

append using "C:\Users\Johanna\Dropbox\Worker rep\kolfu som analysis\ambition vast som.dta", force
describe
save "C:\Users\Johanna\Dropbox\Worker rep\kolfu som analysis\ambition vast som.dta", replace
tab year

* 1998
use "1998 vastsom.DTA", clear 

ren urkon sex
ren subkom muni
ren f84 birth_yr
ren f18 party

tab f12
gen interest = 1 if f12==1 | f12==2
replace interest = 0 if f12==3 | f12==4

tab f35
count if f35==4
gen ambkom = 1 if f35==1 | f35==2
replace ambkom = 0 if f35==3 | f35==4 
* bara kommun

tab f24a
gen bekantpol = 1 if f24a==1
replace bekantpol = 0 if f24a==2
* bara kommunpol

ren f94 class
tab class
gen workerselfid = 1 if class==4 | class==5
replace workerselfid = 0 if workerselfid!=1 & class!=.

tab f89
gen stud = 1 if f89==9
replace stud = 0 if stud!=1 & f89<10

gen workerfam = 1 if f98b==1
replace workerfam = 0 if workerfam!=1 & f98b<10
replace workerfam = . if f98b==0

gen year = 1998

keep birth_yr ambkom bekantpol class workerselfid workerfam stud party year sex muni interest

append using "C:\Users\Johanna\Dropbox\Worker rep\kolfu som analysis\ambition vast som.dta", force
describe
save "C:\Users\Johanna\Dropbox\Worker rep\kolfu som analysis\ambition vast som.dta", replace
tab year

*** 1999 ***

use "1999 vastsom.DTA", clear 

*ren f88 sex
ren kommun muni
gen year = 1999
ren fodyear birth_yr
ren f23 party

tab f15
gen interest = 1 if f15==1 | f15==2
replace interest = 0 if f15==3 | f15==4

*ren f99 class
*ren f104b famclass
*ren f94i student
*ren d42a ambition
*ren f29a bekantpol

tab f42a
gen ambkom = 1 if f42a==1 | f42a==2
replace ambkom = 0 if f42a==3 | f42a==4 
* bara kommun

tab f29a
gen bekantpol = 1 if f29a==1
replace bekantpol = 0 if f29a==2
* bara kommunpol

ren f99 class
tab class
gen workerselfid = 1 if class==4 | class==5
replace workerselfid = 0 if workerselfid!=1 & class!=.
replace workerselfid = . if class >11

tab f94i
gen stud = 1 if f94i==1
replace stud = 0 if f94i==0

gen workerfam = 1 if f104b==1
replace workerfam = 0 if workerfam!=1 & f104b<10
replace workerfam = . if f104b==0


keep birth_yr ambkom bekantpol class workerselfid workerfam stud party year sex muni interest

append using "C:\Users\Johanna\Dropbox\Worker rep\kolfu som analysis\ambition vast som.dta", force
describe
save "C:\Users\Johanna\Dropbox\Worker rep\kolfu som analysis\ambition vast som.dta", replace
tab year

*** 2000 ***

use "2000 vastsom.DTA", clear 

*ren f115b famclass
*ren f111 class
*ren f105i student
*ren f26a ambition
*ren f25a bekantpol
*f115b uppväxt

*ren f98 sex
ren kommun muni
gen year = 2000
ren born birth_yr
ren f24a party

tab f17
gen interest = 1 if f17==1 | f17==2
replace interest = 0 if f17==3 | f17==4


tab f26a
gen ambkom = 1 if f26a==1 | f26a==2
replace ambkom = 0 if f26a==3 | f26a==4 
* b på frågan gäller region

tab f25a
gen bekantpol = 1 if f25a==1
replace bekantpol = 0 if f25a==2
* b är region

ren f111 class
tab class
gen workerselfid = 1 if class==4 | class==5
replace workerselfid = 0 if workerselfid!=1 & class!=.
replace workerselfid = . if class >11

tab f105i
gen stud = 1 if f105i==1
replace stud = 0 if f105i==0

gen workerfam = 1 if f115b==1
replace workerfam = 0 if workerfam!=1 & f115b<10
replace workerfam = . if f115b==0

keep birth_yr ambkom bekantpol class workerselfid workerfam stud party year sex muni interest

append using "C:\Users\Johanna\Dropbox\Worker rep\kolfu som analysis\ambition vast som.dta", force
describe 
save "C:\Users\Johanna\Dropbox\Worker rep\kolfu som analysis\ambition vast som.dta", replace
tab year
* behöver save!

*** 2001 ***

use "2001 vastsom.DTA", clear 

*ren F109B famclass
*ren F106 class
*ren F99I student
*ren F25A ambition
*ren F23A bekantpol
* runt om dessa är bekant i riks och ambition i riks

*ren koen sex
ren kommun muni
gen year = 2001
ren foedaar birth_yr
ren F22 party

tab F15
gen interest = 1 if F15==1 | F15==2
replace interest = 0 if F15==3 | F15==4

tab F25A
gen ambkom = 1 if F25A==1 | F25A==2
replace ambkom = 0 if F25A==3 | F25A==4 
* b på frågan gäller region

tab F23A
gen bekantpol = 1 if F23A==1
replace bekantpol = 0 if F23A==2
* b är region

ren F106 class
tab class
gen workerselfid = 1 if class==4 | class==5
replace workerselfid = 0 if workerselfid!=1 & class!=.
replace workerselfid = . if class >11

tab F99I
gen stud = 1 if F99I==1
replace stud = 0 if F99I==0

gen workerfam = 1 if F109B==1
replace workerfam = 0 if workerfam!=1 & F109B<10
replace workerfam = . if F109B==0

keep birth_yr ambkom bekantpol class workerselfid workerfam stud party year sex muni interest

append using "C:\Users\Johanna\Dropbox\Worker rep\kolfu som analysis\ambition vast som.dta", force
save "C:\Users\Johanna\Dropbox\Worker rep\kolfu som analysis\ambition vast som.dta", replace
tab year


*** 2002 ***

use "2002 vastsom.DTA", clear 

*ren born birth_yr
*ren F98H student
*ren F78 class
*ren F25A ambition
*ren F24A bekantpol
* runt om dessa är bekant i riks och ambition i riks
*ren F19A parti

*ren koen sex
ren kommun muni
gen year = 2002
ren born birth_yr
ren F19A party

tab F13
gen interest = 1 if F13==1 | F13==2
replace interest = 0 if F13==3 | F13==4

tab F25A
gen ambkom = 1 if F25A==1 | F25A==2
replace ambkom = 0 if F25A==3 | F25A==4 
* b på frågan gäller region

tab F24A
gen bekantpol = 1 if F24A==1
replace bekantpol = 0 if F24A==2
* b är region

ren F78 class
tab class
gen workerselfid = 1 if class==4 | class==5
replace workerselfid = 0 if workerselfid!=1 & class!=.
replace workerselfid = . if class >11

tab F98H
gen stud = 1 if F98H==1
replace stud = 0 if F98H==0

* den här surveyen innehåller lite frågor om personröstande

gen workerfam = 1 if F89B==1
replace workerfam = 0 if workerfam!=1 & F89B<10
replace workerfam = . if F89B==0

keep birth_yr ambkom bekantpol class workerselfid workerfam stud party year sex muni interest

append using "C:\Users\Johanna\Dropbox\Worker rep\kolfu som analysis\ambition vast som.dta", force
save "C:\Users\Johanna\Dropbox\Worker rep\kolfu som analysis\ambition vast som.dta", replace
tab year


*** 2003 ***

use "2003 vastsom.DTA", clear 

*ren born birth_yr
*ren F103G student
*ren F81SING class
*ren F27A ambition
*ren F33A bekantpol
* runt om dessa är bekant i riks och ambition i riks
*ren F20B parti

*ren koen sex
ren kommun muni
gen year = 2003
ren born birth_yr
ren F20B party

tab F14
gen interest = 1 if F14==1 | F14==2
replace interest = 0 if F14==3 | F14==4

tab F27A
gen ambkom = 1 if F27A==1 | F27A==2
replace ambkom = 0 if F27A==3 | F27A==4 

tab F33A
gen bekantpol = 1 if F33A==1
replace bekantpol = 0 if F33A==2

ren F81SING class
tab class
gen workerselfid = 1 if class==4 | class==5
replace workerselfid = 0 if workerselfid!=1 & class!=.
replace workerselfid = . if class >11

tab F103G
gen stud = 1 if F103G==1
replace stud = 0 if F103G==0

* cannot find this variable
*gen workerfam = 1 if F89B==1
*replace workerfam = 0 if workerfam!=1 & F89B<10
*replace workerfam = . if F89B==0

* Keep relevant varia
keep birth_yr ambkom bekantpol class workerselfid party stud year sex muni interest

append using "C:\Users\Johanna\Dropbox\Worker rep\kolfu som analysis\ambition vast som.dta", force
save "C:\Users\Johanna\Dropbox\Worker rep\kolfu som analysis\ambition vast som.dta", replace
tab year

stop

**************
** Analysis **
**************

use "C:\Users\Johanna\Dropbox\Worker rep\kolfu som analysis\ambition vast som.dta", clear
tab year

* Create controls in the final format

* variables
bysort year: tab sex
gen woman = 1 if sex==1
replace woman = 0 if sex==2
tab woman

* municipality
tab muni
* okej så länge

* Age

bysort year: sum birth_yr

replace birth_yr = year-birth_yr if year ==1998
replace birth_yr = year-birth_yr if year ==2000
replace birth_yr = year-birth_yr if year ==2002
replace birth_yr = year-birth_yr if year ==2003

replace birth_yr = . if birth_yr <18
replace birth_yr = . if birth_yr >150
sum birth_yr, detail
ren birth_yr age

gen retired = 1 if age>64 & age<150
replace retired = 0 if retired==. & age<150

* age fixed effects
	gen a1 = age<30
	gen a2 = age>29 & age<40
	gen a3 = age>39 & age<50
	gen a4 = age>49 & age<65
	gen a5 = age>64
	tab age if a1==0 & a2 ==0 & a3==0 & a4 ==0 & a5==0

* organize the worker variable
replace workerselfid=. if class ==11
replace workerselfid=. if class ==95
table year if retired==1, c(mean workerselfid)

* set students to non-workers
replace workerselfid = 0 if stud==1


* Indicator for sample 
gen all = 1

** Missing information on party
replace party=. if party==0 
replace party=. if party==98 
replace party=. if party==99 
replace party=. if party==9
replace party=. if party==8 

* students and retired over time
tab stud
table year, c(mean stud mean retired)

* check that distributions look logical over time
table year if stud!=1 & retired!=1, c(mean workerselfid) 
bysort year: tab workerselfid

bysort year: tab bekantpol
table year if stud!=1 & retired!=1, c(mean bekantpol) 


bysort year: tab ambkom
table year if stud!=1 & retired!=1, c(mean ambkom) 

bysort year: tab workerfam
table year if stud!=1 & retired!=1, c(mean workerfam) 



*****************************************************
* Figure 7. Left-hand side estimates  
*****************************************************

* Count observations used in the regressions, i.e., with non-missing data on the controls
keep if party!=. & ambkom!=. & workerselfid!=. & year!=. & muni!=. & woman!=.
keep if a1!=. & a2!=.  & a3!=.   & a4!=.   & a5!=. 
keep if stud!=.
count /* number of observations for Figure 7, population */

*drop estimate-position
local i=0 
local n=0
matrix C =J(17, 5,.)

foreach var in ambkom {
	local n = `n'+1
    local i = `i'+1	 

	foreach sample in all {
	xi: areg `var' workerselfid i.year i.party if `sample'==1, abs(muni)
		
	matrix C[`i',1]=_b[workerselfid]
	matrix C[`i',2]=(_b[workerselfid]+1.96*_se[workerselfid])
	matrix C[`i',3]=(_b[workerselfid]-1.96*_se[workerselfid])
	matrix C[`i',4]=`n'-0.2
	matrix C[`i',5]=1
		
	xi: areg `var' workerselfid stud a1-a5 woman i.party i.year if `sample'==1, abs(muni)

	local i = `i'+1	 
	matrix C[`i',1]=_b[workerselfid]
	matrix C[`i',2]=(_b[workerselfid]+1.96*_se[workerselfid])
	matrix C[`i',3]=(_b[workerselfid]-1.96*_se[workerselfid])
	matrix C[`i',4]=`n'+0.2
	matrix C[`i',5]=2
	matrix list C
	}
}	

	
matrix list C

		svmat C
		rename C1 estimate
		rename C2 upper
		rename C3 lower
		rename C4 order
		rename C5 position

twoway (rbar upper lower order if position==1, fcolor(black) lcolor(none) barwidth(.005)) ///
 (scatter estimate order if position==1, mcolor(black) msize(large) msymbol(circle)) ///
 (rbar upper lower order if position==2, fcolor(gs7) lcolor(gs7) barwidth(.005)) ///
 (scatter estimate order if position==2, mcolor(gs7) msize(large) msymbol(circle) ///
 graphregion(color(white)) xtitle("") ytitle("Workers v Non-Workers (Ppt)") ///
 ysc(r(-0.2 0)) ylabel(-0.2(0.05)0, angle(horizontal)) xsc(r(0.5 3.5)) xlabel(1(1)3) xsize(5) ///
 ysize(2) yline(0, lpattern(longdash) lcolor(gs7)))

******************************************* 
* Figure W11 
******************************************* 

count if party!=. & interest!=. & workerselfid!=. & year!=. & muni!=. & woman!=.
count if party!=. & bekantpol!=. & workerselfid!=. & year!=. & muni!=. & woman!=.
sum interest
sum bekantpol

local i=0 
local n=0
matrix C =J(17, 5,.)

foreach var in interest bekantpol {
	local n = `n'+1
    local i = `i'+1	 

	foreach sample in all {
	xi: areg `var' workerselfid i.year i.party if `sample'==1, abs(muni)
		
	matrix C[`i',1]=_b[workerselfid]
	matrix C[`i',2]=(_b[workerselfid]+1.96*_se[workerselfid])
	matrix C[`i',3]=(_b[workerselfid]-1.96*_se[workerselfid])
	matrix C[`i',4]=`n'-0.2
	matrix C[`i',5]=1
		
	xi: areg `var' workerselfid stud a1-a5 woman i.party i.year if `sample'==1, abs(muni)

	local i = `i'+1	 
	matrix C[`i',1]=_b[workerselfid]
	matrix C[`i',2]=(_b[workerselfid]+1.96*_se[workerselfid])
	matrix C[`i',3]=(_b[workerselfid]-1.96*_se[workerselfid])
	matrix C[`i',4]=`n'+0.2
	matrix C[`i',5]=2
	matrix list C
	}
}	

matrix list C

		svmat C
		rename C1 estimate
		rename C2 upper
		rename C3 lower
		rename C4 order
		rename C5 position

twoway (rbar upper lower order if position==1, fcolor(black) lcolor(none) barwidth(.005)) ///
 (scatter estimate order if position==1, mcolor(black) msize(large) msymbol(circle)) ///
 (rbar upper lower order if position==2, fcolor(gs7) lcolor(gs7) barwidth(.005)) ///
 (scatter estimate order if position==2, mcolor(gs7) msize(large) msymbol(circle) ///
 graphregion(color(white)) xtitle("") ytitle("Workers v Non-Workers (Ppt)") ///
 ysc(r(-0.2 0)) ylabel(-0.2(0.05)0, angle(horizontal)) xsc(r(0.5 3.5)) xlabel(1(1)3) xsize(5) ///
 ysize(2) yline(0, lpattern(longdash) lcolor(gs7)))

